import { Injectable } from '@nestjs/common';
import { CreateFeatureDto } from './dto/create-feature.dto';
import { UpdateFeatureDto } from './dto/update-feature.dto';
import { PrismaService } from 'src/prisma/prisma.service';
import * as _ from 'lodash';
 
@Injectable()
export class FeaturesService {
  constructor(private prisma: PrismaService) {}

  async create(createFeatureDto: CreateFeatureDto) {
    // 首先查询sort的最大值
    const maxSort = await this.prisma.testFeatures.aggregate({
      _max: {
        sort: true,
      },
    });
    createFeatureDto.sort = maxSort._max.sort + 1;
    await this.prisma.testFeatures.create({
      data: createFeatureDto,
    });
    return true;
  }

  async findAll(searchKey: string) {
    let _w = {};
    if (searchKey) {
      _w = {
        feature: {
          contains: searchKey,
        },
      };
    }
    const data = await this.prisma.testFeatures.findMany({
      where: _w,
      // 指定查询字段
      select: {
        id: true,
        feature: true,
        sort: true,
      },
      orderBy: [
        {
          sort: 'asc',
        },
      ],
    });
    return data;
  }

  async findOne(id: number) {
    return await this.prisma.testFeatures.findFirst({
      where: { id: id },
      select:{
        id:true,
        sort:true,
        hasPreAction:true,
        feature:true
      }
    });
  }

  //
  async featureIsExists(feature: string) {
    const data = await this.prisma.testFeatures.findFirst({
      where: { feature: feature },
    });
    return data !== null;
  }

  //更新features
  async update(id: number, updateFeatureDto: UpdateFeatureDto) {
    // console.log("id---", id, typeof id);
    return this.prisma.testFeatures.update({
      where: { id },
      data: updateFeatureDto,
    });
  }

  //删除features
  async remove(id: number) {
    return await this.prisma.testFeatures.delete({
      where: { id: id },
    });
  }

  //根据Feature 查询下属的scenarios
  async queryScenarioByFeature(feature: string) {
    const featureData = await this.prisma.testFeatures.findFirst({
      where: {
        feature: feature,
      }
    });
    const fid = featureData.id;
    const res = await this.prisma.featureScenario.findMany({
      where: {
        fid: fid,
      },
      select: {
        scenario: {
          select: {
            scenario: true,
            description: true,
            cases: {
              select:{
                elementId:true,
                sort:true,
                type:true,
                value:true,
                description:true
              },
              orderBy:{
                sort:"asc"
              }
            },
          },
        },
      },
      orderBy:{
        sort:'asc'
      }
  
    });
    return res;
  }

  // 根据featureId,  查询下属的所有scenarios
  async queryScenariosByFeatureId(featureId: number) {
    return await this.prisma.testFeatures.findFirst({
      where: { id: featureId },
      select: {
        feature: true,
        hasPreAction:true,
        id:true,
        sort:true,
        scenarios: {
          select: {
            sort: true,
            scenario: {
              // 通过include把统计到的cases与scenario字段放到一个层级
              include:{
                _count:{
                  select:{
                    cases:true
                  }
                }
              }
            },
          },
          orderBy: {
            sort: 'asc',
          },
        },
      },
    });
  }

  async findAllScenario() {
    const data = await this.prisma.testFeatures.findMany({
      // 指定查询字段
      select: {
        id: true,
        feature: true,
        sort: true,
      },
    });
    return data;
  }

  async findFeatureScenariosMap() {
    const data = await this.prisma.testFeatures.findMany({
      select: {
        feature: true,
        hasPreAction:true,
        scenarios: {
          select: {
            scenario: true,
          },
          orderBy:{
            sort:'asc'
          }
        },
      },
    });
    return data;
  }

 
}
